<template>
  <div class='login-container'>
    <!-- 登录头部导航栏 -->
    <van-nav-bar title="登录" style="background-color: #3296fa;">
      <van-icon slot="left" name="cross" @click="$router.back()"/>
    </van-nav-bar>
    <!-- 提示框 -->
    <van-notice-bar left-icon="volume-o" scrollable text="万能登录手机号及验证码是 mobile:13911111111/17090086870, code:246810" />
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field v-model="user.mobile" type="number" left-icon="phone-o" name="mobile" placeholder="请输入手机号" :rules="userFormRules.mobile" />
      <van-field v-model="user.code" type="number" left-icon="comment-o" name="code" placeholder="请输入短信验证码" :rules="userFormRules.code">
        <template #button>
          <van-button size="small" type="default" native-type="button" style="background-color: #ededed;" @click="onSend" v-if="isShow">发送验证码</van-button>
          <!-- 倒计时 -->
          <van-count-down v-if="!isShow" :time="time" format="ss 秒" @finish="finish" />
        </template>
      </van-field>
      <div style="padding: 53px 33px;">
        <van-button block type="info" native-type="submit" style="background-color: #6db4fb;">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user.js'
export default {
  name: 'loginIndex',
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      // 校验规则
      userFormRules: {
        mobile: [
          { required: true, message: '输入不能为空' },
          { pattern: /^(?:(?:\+|00)86)?1\d{10}$/, message: '请正确输入手机号' }
        ],
        code: [
          { required: true, message: '请填写用户名' },
          { pattern: /\d{6}/, message: '验证码格式错误' }
        ]
      },
      time: 1000 * 60,
      isShow: true
    }
  },
  components: {},
  methods: {
    // 表单提交请求
    async onSubmit () {
      const user = this.user
      // 在组件中必须通过this.$toast来调用Toast组件
      this.$toast.loading({
        message: '登录中',
        forbidClick: true,
        duration: 0 // 默认2秒
      })
      // 表单请求
      try {
        const res = await login(user)
        this.$toast.success('登录成功')
        this.$store.commit('setUser', res.data.data)
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码错误')
        } else {
          this.$toast.fail('登录失败,请重新登录')
        }
      }
    },

    // 发送验证码
    async onSend () {
      // 校验手机号
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log('手机号验证成功')
        this.isShow = false
      } catch (err) {
        return console.log('手机号验证失败', err)
      }
      // 请求发送验证码
      try {
        const res = await sendSms(this.user.mobile)
        console.log('验证码发送成功结果')
        console.log(res)
        this.$toast.success('发送成功')
      } catch (err) {
        this.isShow = true
        this.$toast.fail('发送失败,请稍后重试')
      }
    },

    // 倒计时结束后的调用的
    finish () {
      this.isShow = true
    }
  },
  created () {},
  mounted () {}
}
</script>

<style lang='less' scoped>
.login-container {
  .van-button::before {
    border: none;
  }
  .van-nav-bar .van-icon {
    color: #fff;
}
}
</style>
